import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Util {
    public static Image getImage(String path) {
        return new ImageIcon(Util.class.getResource(path)).getImage();
    }
    public static Icon getIcon(String path){
        return new ImageIcon(Util.class.getResource(path));
    }
    /**
     * 创建数据库连接
     */
    public static Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "yks957");
            System.out.println("con:" + con);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    public static ArrayList query() {
        Connection con = getConnection();
        Statement statement = null;
        ResultSet res = null;
        ArrayList<User> personlist = new ArrayList();
        try {
            statement = con.createStatement();
            String str = "select u_name,u_password,u_money from t_user";
            res = statement.executeQuery(str);
            while (res.next()) {
                User user = new User();
                user.setName(res.getString("u_name"));
                user.setPwd(res.getString("u_password"));
                user.setMoney(res.getInt("u_money"));
                personlist.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, res);
        }
        return personlist;
    }
    public static void close(Connection con, Statement statement, ResultSet res) {
        if (res != null) {
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static void insert(String sql) {
        Connection con = getConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();
            int row = statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (statement !=null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
